<?php
/**
 * File: Book.php
 * User: Mr.Yang
 * Date: 2025/3/22 0022
 * Time: 16:35
 * Note:
 */

namespace app\home\controller;

use app\admin\model\Book as BookModel;
use app\admin\model\Category as CategoryModel;

class Book extends Base
{
    /**
     * Note: 图书详情
     * User: Mr.Yang
     * @param int $bookid
     * @return mixed
     */
    public function info($bookid = 0)
    {
        //获取图书相信信息
        $info = BookModel::where([['id', '=', $bookid], ['status', '=', 1]])->findOrEmpty();
        if (!$info) {
            $this->redirect(url('index/index'));
        }
        //获取图书分类
        $catinfo = CategoryModel::where('id', '=', $info['catid'])->find();
        $this->assign('bookinfo', $info);
        $this->assign('catinfo', $catinfo);
        $this->assign('uid', session('userid'));
        return $this->fetch();
    }

    /**
     * Note: 搜索图书
     * User: Mr.Yang
     * @return mixed
     */
    public function search()
    {
        $keyword = $this->request->get('keyword', '');
        $order = $this->request->get('order', 'id');
        if (!$keyword) {
            $booklist = array();
            $page = null;
            $total = 0;
        }else{
            $where = [['status', '=', 1], ['bname', 'like', "%$keyword%"]];
            $booklist = BookModel::where($where)->order($order, 'desc')->paginate();
            $page = $booklist->render();
            $total = $booklist->total();
        }

        $this->assign('list', $booklist);
        $this->assign('page', $page);
        $this->assign('keyword', $keyword);
        $this->assign('total', $total);
        $this->assign('order', $order);
        return $this->fetch();
    }
}